
//从仓库获取用户名，密码
$(function(){
    users=JSON.parse(localStorage.getItem("users"));
    // console.log(users[0]);
    // console.log(users[0].username);
});

//登录
$(function(){
    var arrs1=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",1,2,3,4,5,6,7,8,9,0]
    $("button:eq(0)").click(function(){//随机验证码
        // console.log(Math.floor(Math.random()*62));//产生0-61的索引
        $("button:eq(0)").html(arrs1[Math.floor(Math.random()*62)]+arrs1[Math.floor(Math.random()*62)]+arrs1[Math.floor(Math.random()*62)]+arrs1[Math.floor(Math.random()*62)]);
    });
// });

// $(function(){
    $("button:eq(1)").click(function(){//用户名密码验证
        inputValue1=$("input:eq(0)").val();
        inputValue2=$("input:eq(1)").val();
        inputValue3=$("input:eq(2)").val();
        var usernames=[];
        for(var i=0;i<users.length;i++){
            usernames.push(users[i].username);
        }
        var index=usernames.indexOf(inputValue1);//在名字列表中找输入名字对应的索引,可根据此索引找对应的密码
        
        
        layui.use('form',function(){
            var form=layui.form;
            form.verify({
                username:function(value){
                    if(inputValue1==''){
                        return "请输入用户名"
                    }
                    if(inputValue2==''){
                        return "请输入密码"
                    }
                    if(inputValue3==''){
                        return "请输入验证码"
                    }
                    if($("button:eq(0)").html()!=inputValue3){
                        return "请输入正确的验证码"
                    }
                    if(index==-1){
                        return "此用户不存在"
                    }else{
                        if(inputValue2==users[index].password && inputValue1=="admin"){
                            location.href="页面.html";
                        }else if(inputValue2!=users[index].password && inputValue1=="admin"){
                            return "密码输入错误"
                        }
                        else if(inputValue2==users[index].password && inputValue1!="admin"){
                            location.href="普通用户.html";
                            localStorage.setItem("username",inputValue1);
                            localStorage.setItem("password",inputValue2);
                        }else if(inputValue2!=users[index].password && inputValue1!="admin"){
                            return "密码输入错误"
                        }
                    }
                },
                //后面两个密码和验证码校验可能不需要，因为前面写好了
                password:function(value){
                    if($("input[name=password]").val()==""){
                        return "请输入密码"
                    }
                },
                yzm:function(value){
                    if($("button:eq(0)").html()!=value){
                        return "请输入正确的验证码"
                    }
                }
            });
            form.on('submit(formDemo)',function(data){
                console.log(data);
            });
        });



        // console.log(usernames);//得到所有的名字
        // var index=usernames.indexOf(inputValue1);
        // if(index==-1){
        //     alert("用户名输入错误");
        // }else{
        //     if(inputValue1=="admin"){
        //         if(inputValue2=="123456"){
        //             if(inputValue3==$("button:eq(0)").html()){
        //                 location.href="页面.html";
        //             }else{
        //                 alert("验证码输入错误");
        //             }
        //         }else{
        //             alert("密码输入错误");
        //         }
        //     }else{
        //         if(inputValue2==users[index].password && inputValue1!="admin"){
        //             if(inputValue3==$("button:eq(0)").html()){
        //                 location.href="普通用户.html";
        //                 localStorage.setItem("password",inputValue2);
        //                 localStorage.setItem("username",inputValue1);
        //             }else{
        //                 alert("验证码输入错误");
        //             }
        //         }else{
        //             alert("密码输入错误");
        //         }
        //     }
        // }

    });
});



